<template>
	<view class="orders-list" :class="[config.style]">
		<!-- 顶部固定部分 -->
		<view class="top-warp">
			<view class="tabs-block">
				<view class="tabs">
					<ListTabs v-model="tabIndex" :config="config" :tabs="tabs" @change="tabChange"></ListTabs>
				</view>
			</view>
		</view>

		<!--数据列表部分-->
		<view class="list-section uni-clearfix">
			<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" top="78" :down="downOption" @down="downCallback" @up="upCallback">
				<!-- 数据列表 -->
				<view class="list">
					<view class="item" v-for="(item,index) in list" :key="index" >
						<view class="base-info">
							<view class="orderno">
								<text>订单号：</text>
								<text>{{item.order_no}}</text>
							</view>
							<view class="evaluate" v-if="paid == 1" >
								<text v-if="item.evaluate.update_time==item.evaluate.create_time" class="evaluate-btn" @click="evaluateForm(item.order_no)">评价</text>
								<text v-else class="evaluate-btn-disable">已评价</text>
							</view>
						</view>
						<view class="content" @click="goToDetailLink(item.order_info_id,item.order_info_type)">
							<view class="cover">
								<image mode="widthFix" :src="item.order_info.cover_400" @error="imageError" class="image"></image>
							</view>
							<view class="info">
								<view class="title text-ellipsis-2">{{item.order_info.title}}</view>
								<view class="description text-ellipsis">{{item.order_info.description}}</view>
								<view class="price">
									<text class="price-title">总价：</text>
									<text>￥ {{item.order_info.price == 0 ? '免费':item.order_info.price}}</text>	
								</view>
							</view>
						</view>
						<view class="pay-info">
							<view class="pay-yes" v-if="paid == 1">
								<view class="paid-price">
									<text>实付：</text>
									<text>￥ {{item.paid_fee}}</text>
								</view>
								<view class="paid-time">
									<text>支付时间：</text>
									<text>{{item.paid_time_str}}</text>
								</view>
							</view>
							<view class="pay-yes" v-if="paid == 0">
								<view class="paid-price">
									<text>应付：</text>
									<text>￥ {{item.paid_fee}}</text>
								</view>
								<view class="pay-btn">
									<button type="default" size="mini" @click="pay('muu_classroom',item.order_no)">{{config.sale_btn.before}}</button>
								</view>
							</view>
						</view>
					</view>
				</view>
			</mescroll-uni>
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import ListTabs from "../../common/listTabs"; //列表tab

	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			ListTabs,
		},
		data() {
			return {
				urlParams: {}, //url传递过来的参数
				page: 1, //当前页码
				list: [], //列表数据
				tabs: ['已支付', '未支付'],
				tabIndex: 0 ,// tab下标
				paid: 1, // 默认已支付状态
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
			}
		},
		onLoad(option) {
			//获取url参数
			this.getUrlParam()
		},
		created() {
			
		},
		computed: {
			...mapState(['config'])
		},
		methods: {
			
			//获取url参数
			getUrlParam: function(){
				// #ifdef MP-WEIXIN
				console.log('小程序端执行')
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let curRoute = routes[routes.length - 1].route //获取当前页面路由
				let curParam = routes[routes.length - 1].options; //获取路由参数
				this.urlParams = curParam
				// #endif
			
				// #ifdef H5
				let curParam = this.$route.query;
				this.urlParams = curParam
				// #endif
				
			},
			//上拉加载的回调
			upCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				console.log(keyword)
				let paid = 1
				if(this.tabIndex == 0){
					paid = 1
				}
				if(this.tabIndex == 1){
					paid = 0
				}
				let data = this.urlParams
					data.action = 'lists'
					data.m = 'muu_classroom'
					data.page = this.page
					data.paid = paid
				//获取数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/orders'
					//#endif
					//#ifdef H5
					url = 'entry/site/orders'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							let list = res.data.data.data
							that.list = that.list.concat(list);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(list.length);
							that.page = that.page + 1
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				let paid = 1
				if(this.tabIndex == 0){
					paid = 1
				}
				if(this.tabIndex == 1){
					paid = 0
				}
				
				this.page = 1
				
				let data = this.urlParams
					data.action = 'lists'
					data.m = 'muu_classroom'
					data.page = this.page
					data.paid = paid
				//获取数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/orders'
					//#endif
					//#ifdef H5
					url = 'entry/site/orders'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.list = []
							that.list = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.list.length);
							that.page = 2
						}
					}
				})
			},
			
			// 切换TAB
			tabChange: function(index) {
				console.log(this.urlParams)
				this.page = 1
				this.list = []// 先置空列表,显示加载进度
				if(index == 0){
					this.paid = 1
				}
				if(index == 1){
					this.paid = 0
				}
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			
			/**
			 * 进入相应类型课程详情页
			 * @param {Object} id
			 * @param {Object} type
			 */
			goToDetailLink: function(id,type){
				let urlParams = 'id='+id
				let page = ''
				if(type == 'Knowledge'){
					page = '../../knowledge/detail'
				}
				if(type == 'KnowledgeColumn'){
					page = '../../column/detail'
				}
				if(type == 'Live'){
					page = '../../live/detail'
				}
				if(type == 'Offline'){
					page = '../../offline/detail'
				}
				if(type == 'Material'){
					page = '../../material/detail'
				}
				if(type == 'ExamPaper'){
					page = '../../exam/paper/detail'
				}
				let url = page + '?' + urlParams
				console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
			
			/**
			 * 订单支付
			 */
			pay: function(module,order_no){
				let that = this
				// 支付
				let data = {}
					data.module = module
					data.order_no = order_no
				let url = ''
					// #ifdef MP-WEIXIN
					url = 'entry/wxapp/pay'
					// #endif
					// #ifdef H5
					url = 'entry/site/pay'
					data.action = 'jssdk'
					// #endif
				// 支付
				this.util.request({
					url : url,
					data : data,
					
					success(res) {
						if (res.data && res.data.data && res.data.code == 200) {
							console.log(res);
							// #ifdef H5
							//JSSDK
							that.wechat.pay({
								'timeStamp': res.data.data.timeStamp,
								'nonceStr': res.data.data.nonceStr,
								'package': res.data.data.package,
								'signType': res.data.data.signType,
								'paySign': res.data.data.paySign,
								'success': function (res) {
									//执行支付成功提示
									uni.showModal({
									    title: '提示',
									    content: '支付成功',
										confirmText: '查看订单',
										cancleText: '返回课程',
									    success: function (res) {
									        if (res.confirm) {
									            console.log('用户点击确定');
												let order_url = '../my/orders/index'
												uni.redirectTo({
													url: order_url
												});
									        } else if (res.cancel) {
									            console.log('用户点击取消');
												uni.navigateBack()
									        }
									    }
									});
								},
								'fail': function (res) {
									uni.navigateBack()
								}
							},window.location.href);
							// #endif
							
							// #ifdef MP-WEIXIN
							//发起支付
							uni.requestPayment({
								'provider': 'wxpay',
								'timeStamp': res.data.data.timeStamp,
								'nonceStr': res.data.data.nonceStr,
								'package': res.data.data.package,
								'signType': res.data.data.signType,
								'paySign': res.data.data.paySign,
								'success': function (res) {
									//执行支付成功提示
									// 更新父组件数据
									that.$emit('getData')
								},
								'fail': function (res) {
									
								}
							})
							// #endif
							
						 }else{
							uni.showModal({
								title: '系统提示',
								content: res.data.msg ? res.data.msg : '错误',
								showCancel: false,
								success: function (res) {
									
								}
							})
						}
					},
					fail(res) {
						uni.showModal({
							title: '系统提示',
							content: res.data.msg ? res.data.msg : '错误',
							showCancel: false,
							success: function (res) {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					}
				});
			},
			
			/**
			 * 发表评价
			 */
			evaluateForm: function(order_no){
				console.log(order_no)
				let url = '../../evaluate/edit?' + 'order_no=' + order_no
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	
	/*顶部固定部分*/
	.top-warp {
		background-color: #ffffff;
		.tabs-block {
			display: flex;
			flex-wrap: wrap;
			margin: 0 20rpx;
			.tabs {
				flex: 1;
			}
		}
	}
	.list {
		/*小图*/
		padding: 0 10rpx;
		display: block;

		.item {
			box-sizing:border-box;
			background-color: #fff;
			margin: 10rpx;
			border-radius: 10rpx;
			box-shadow: 0 0 10rpx rgba(0,0,0,.02);
			overflow: hidden;
			.base-info {
				display: flex;
				font-size: 28rpx;
				border-bottom: 2rpx solid #eee;
				line-height: 64rpx;
				color: #666;
				padding: 0 20rpx;
				.orderno {
					flex: 1;
				}
				.evaluate {
					width: 120rpx;
					text-align: center;
					.evaluate-btn {
						color: #333;
					}
					.evaluate-btn-disable {
						color: #999;
					}
				}
			}
			.content {
				display: flex;
				padding: 20rpx;
				.cover {
					width: 280rpx;
					overflow: hidden;
					.image {
						border-radius: 10rpx;
						width: 100%;
						height: auto;
						display: block;
					}
				}
				.info {
					flex: 1;
					padding: 0 0 0 20rpx;
					.title {
						font-size: 28rpx;
						color: #000;
						font-weight: 600;
						height: 72rpx;
						line-height: 36rpx;
					}
					.description {
						color: #999;
						font-size: 24rpx;
					}
					.price {
						margin-top: 20rpx;
						font-size: 28rpx;
						text-align: right;
					}
					.exp-info {
						margin-top: 10rpx;
						color: #999;
						font-size: 24rpx;
						.item {
							display: inline;
							margin: 0;
							.num {
								margin: 0 10rpx;
							}
						}
					}
				}
			}
			.pay-info {
				border-top: 2rpx solid #eee;
				line-height: 64rpx;
				color: #666;
				padding: 0 20rpx;
				.pay-yes {
					display: flex;
					font-size: 28rpx;
					.paid-price {
						flex: 1;
					}
					.paid-time {
						width: 440rpx;
						text-align: right;
					}
					.pay-btn {
						width: 300rpx;
						
						button {
							display: block;
							margin: 6rpx auto;
							font-size: 24rpx;
							color: #fff;
						}
					}
				}
			}
		}
	}
	
	.Blue {
		.price {
			color: $Blue;
		}
		.paid-price {
			color: $Blue;
		}
		.pay-btn button{
			background-color: $Blue;
		}
	}
	.Green {
		.price {
			color: $Green;
		}
		.paid-price {
			color: $Green;
		}
		.pay-btn button{
			background-color: $Green;
		}
	}
	.LightRed {
		.price {
			color: $LightRed;
		}
		.paid-price {
			color: $LightRed;
		}
		.pay-btn button {
			background-color: $LightRed;
		}
	}
	.Orange {
		.price {
			color: $Orange;
		}
		.paid-price {
			color: $Orange;
		}
		.pay-btn button {
			background-color: $Orange;
		}
	}
	.LightPink {
		.price {
			color: $LightPink;
		}
		.paid-price {
			color: $LightPink;
		}
		.pay-btn button {
			background-color: $LightPink;
		}
	}
	.Magenta {
		.price {
			color: $Magenta;
		}
		.paid-price {
			color: $Magenta;
		}
		.pay-btn button {
			background-color: $Magenta;
		}
	}
</style>
